package com.dev.security.setting.service.impl;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dev.security.setting.beans.UserSetting;
import com.dev.security.setting.beans.UserSettingId;
import com.dev.security.setting.dao.UserSettingRepository;
import com.dev.security.setting.service.IUserSettingService;

/**
 * @author 飘渺青衣
 * @see
 */
@Service
public class UserSettingService implements IUserSettingService {

	@Autowired
	private UserSettingRepository userSettingRepository;
	
	/* (non-Javadoc)
	 * @see com.dev.security.setting.service.IUserSettingService#save(com.dev.security.setting.beans.UserSetting)
	 */
	@Override
	public void save(UserSetting setting) {
		this.userSettingRepository.save(setting);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.setting.service.IUserSettingService#findUserSetting(java.lang.String, java.lang.String)
	 */
	@Override
	public UserSetting findUserSetting(String account_id, String field_name) {
		return this.userSettingRepository.findOne(new UserSettingId(account_id, field_name));
	}

	/* (non-Javadoc)
	 * @see com.dev.security.setting.service.IUserSettingService#findUserSettings(java.lang.String)
	 */
	@Override
	public List<UserSetting> findUserSettings(String account_id) {
		return this.userSettingRepository.findAllByAccountId(account_id);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.setting.service.IUserSettingService#findUserSettingMap(java.lang.String)
	 */
	@Override
	public Map<String, String> findUserSettingMap(String account_id) {
		Map<String, String> result = new LinkedHashMap<String, String>();
		Iterator<UserSetting> it = this.findUserSettings(account_id).iterator();
		UserSetting us = null;
		while(it.hasNext()) {
			us = it.next();
			result.put(us.getId().getField_name(), us.getValue());
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.setting.service.IUserSettingService#remove(java.lang.String, java.lang.String)
	 */
	@Override
	public void remove(String account_id, String field_name) {
		this.userSettingRepository.delete(new UserSettingId(account_id, field_name));
	}

}
